from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import (
    RetrieveModelMixin, DestroyModelMixin, CreateModelMixin, ListModelMixin, UpdateModelMixin)

from my_chat.models import UserProfile, Friend


class RegisterView(CreateModelMixin, GenericViewSet):
    """注册"""
    serializer_class = []
    queryset = UserProfile.objects.all()
    permission_classes = []


class ResetPasswordView(UpdateModelMixin, GenericViewSet):
    """重置密码"""
    serializer_class = []
    queryset = UserProfile.objects.all()


class SearchFriendsView(ListModelMixin, GenericViewSet):
    """搜索好友"""
    serializer_class = []

    def get_queryset(self):
        """爱好搜索/昵称搜索/邮箱搜索好友"""
        happy_list = self.request.data.get('happy', None)
        nick_name = self.request.data.get('nick_name', None)
        email = self.request.data.get('nick_name', None)
        queryset = None
        if happy_list:
            queryset = UserProfile.objects.filter(hobby__in=happy_list)

        if nick_name:
            queryset = UserProfile.objects.filter(nick_name__icontains=nick_name)

        if email:
            queryset = UserProfile.objects.filter(email=email)

        return queryset or UserProfile.objects.filter(is_delete=False)[:20]


class AddDeleteFriendsView(CreateModelMixin, GenericViewSet):
    """添加/删除好友"""
    serializer_class = []
    queryset = Friend.objects.all()

